/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.yy.bugzillahelp.ui.server;

import com.yy.bugzillahelp.action.download.BugCrawlAction;
import com.yy.bugzillahelp.data.server.ServerData;
import java.awt.Container;
import java.awt.Window;
import javax.swing.JOptionPane;

/**
 *
 * @author hualun-alan
 */
public class ServerInfoPanel extends javax.swing.JPanel {

    private ServerData connectData = new ServerData();
    private boolean okAction = false;

    /**
     * Creates new form ServerInfoPanel
     */
    public ServerInfoPanel() {
        initComponents();
    }

    public void setConnectData(ServerData connectData) {
        this.connectData = connectData;
        jtfConnectName.setText(connectData.getConnectName());
        jtfServerURL.setText(connectData.getServerUrl());
        jtfUserName.setText(connectData.getUserName());
        jtfPassword.setText(connectData.getPassword());
    }

    /**
     * This method is called from within the constructor to initialize the form. WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jtfConnectName = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jtfServerURL = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jtfUserName = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jtfPassword = new javax.swing.JTextField();
        jbValidation = new javax.swing.JButton();
        jbOK = new javax.swing.JButton();
        jbCancel = new javax.swing.JButton();

        jLabel1.setText("Connect Name: ");

        jtfConnectName.setColumns(20);

        jLabel2.setText("Server URL : ");

        jtfServerURL.setColumns(20);

        jLabel3.setText("User Name : ");

        jtfUserName.setColumns(20);

        jLabel4.setText("User Password : ");

        jtfPassword.setColumns(20);

        jbValidation.setText("Validation");
        jbValidation.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbValidationActionPerformed(evt);
            }
        });

        jbOK.setText("OK");
        jbOK.setPreferredSize(new java.awt.Dimension(80, 23));
        jbOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbOKActionPerformed(evt);
            }
        });

        jbCancel.setText("Cancel");
        jbCancel.setPreferredSize(new java.awt.Dimension(80, 23));
        jbCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbCancelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(70, 70, 70)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jtfUserName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jtfConnectName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jtfServerURL, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jtfPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jbValidation)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jbOK, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbCancel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(98, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(49, 49, 49)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jtfConnectName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jtfServerURL, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jtfUserName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jtfPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jbValidation)
                .addGap(30, 30, 30)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbOK, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jbCancel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(63, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jbCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbCancelActionPerformed
        okAction = false;
        closeWindow();
    }//GEN-LAST:event_jbCancelActionPerformed

    private void jbOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbOKActionPerformed
        okAction = true;
        connectData.setConnectName(jtfConnectName.getText());
        connectData.setServerUrl(jtfServerURL.getText());
        connectData.setUserName(jtfUserName.getText());
        connectData.setPassword(jtfPassword.getText());
        System.out.println(connectData);
        closeWindow();
    }//GEN-LAST:event_jbOKActionPerformed

    private void jbValidationActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbValidationActionPerformed
        boolean avilable = BugCrawlAction.checkUrlAvilable(jtfServerURL.getText().trim());
        if (avilable) {
            JOptionPane.showMessageDialog(this, "Connect to server successfully!", "Message", JOptionPane.PLAIN_MESSAGE);
        } else {
            JOptionPane.showMessageDialog(this, "Connect to server failed!", "Warning", JOptionPane.WARNING_MESSAGE);
        }
    }//GEN-LAST:event_jbValidationActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JButton jbCancel;
    private javax.swing.JButton jbOK;
    private javax.swing.JButton jbValidation;
    private javax.swing.JTextField jtfConnectName;
    private javax.swing.JTextField jtfPassword;
    private javax.swing.JTextField jtfServerURL;
    private javax.swing.JTextField jtfUserName;
    // End of variables declaration//GEN-END:variables

    private void closeWindow() {
        Container parent = this.getParent();
        while (!(parent instanceof Window)) {
            parent = parent.getParent();
        }
        if (parent instanceof Window) {
            ((Window) parent).dispose();
        }
    }

    public ServerData getConnectData() {
        return connectData;
    }

    public boolean isOkAction() {
        return okAction;
    }
}
